// src/api/account.js
import { api } from './index'

/**
 * 统一的账户域 API
 * - 注意：保持既有方法签名和路径不变，避免影响 Whitelist / MailAccounts 等其它模块。
 */
export const accountApi = {
    // -------- Profile / Security --------
    getProfile:     () => api.get('/account/profile'),
    updateProfile:  (body) => api.put('/account/profile', body),
    changePwd:      (body) => api.put('/account/password', body),

    // -------- Whitelist --------
    listWhitelist:  () => api.get('/account/whitelist'),
    addWhitelist:   (body) => api.post('/account/whitelist', body),
    updWhitelist:   (id, body) => api.put(`/account/whitelist/${id}`, body),
    delWhitelist:   (id) => api.del(`/account/whitelist/${id}`),

    // -------- Mail Accounts --------
    listMailAcc:    () => api.get('/account/mail-accounts'),
    createMailAcc:  (body) => api.post('/account/mail-accounts', body),
    updateMailAcc:  (id, body) => api.put(`/account/mail-accounts/${id}`, body),
    deleteMailAcc:  (id) => api.del(`/account/mail-accounts/${id}`),
    testMailAcc:    (id) => api.post(`/account/mail-accounts/${id}/test`),

    // -------- Subscription --------
    getCurrentSub:  () => api.get('/account/subscription/current'),
    listPlans:      () => api.get('/account/plans'),

    /**
     * 购买订阅（创建订单并返回支付宝收银台地址）
     * 向后兼容两种调用方式：
     *  1) 旧：purchasePlan({ planId, period?, subject?, autoRenew? })
     *  2) 新：purchasePlan(planId, period?, subject?, autoRenew=false)
     * 返回：{ orderNo, payUrl }
     */
    purchasePlan:   (arg1, period, subject, autoRenew = false) => {
        let payload
        if (typeof arg1 === 'object' && arg1 !== null) {
            // 兼容旧签名：直接传对象
            payload = { ...arg1 }
        } else {
            // 新签名：逐参传入
            payload = { planId: arg1 }
            if (period) payload.period = period            // 'MONTHLY' | 'QUARTERLY' | 'YEARLY'
            if (subject) payload.subject = subject        // 可选：收银台标题
            if (autoRenew) payload.autoRenew = true       // 可选：自动续费（默认 false）
        }
        return api.post('/account/subscription/purchase', payload)
    },

    enableAutoRenew: () => api.post('/account/subscription/auto-renew/enable'),
    cancelAutoRenew: () => api.post('/account/subscription/auto-renew/cancel'),

    // -------- Usage --------
    getUsageSummary: () => api.get('/account/usage/summary'), // { monthOutbound, totalOutbound, last24hOutbound, byDay:[{date,count}] }
    getUsageLogs:    (page = 1, size = 20) => api.get('/account/usage/logs', { page, size })
}
